package org.zky.modules.merchant.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.merchant.domain.ZkyMechanismTab;
import org.zky.modules.merchant.repository.ZkyMechanismTabRepository;
import org.zky.modules.merchant.service.ZkyMechanismTabService;
import org.zky.modules.merchant.service.dto.ZkyMechanismTabDto;
import org.zky.modules.merchant.service.dto.ZkyMechanismTabQueryCriteria;
import org.zky.modules.merchant.service.mapper.ZkyMechanismTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyMechanismTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyMechanismTabServiceImpl implements ZkyMechanismTabService {

    private final ZkyMechanismTabRepository zkyMechanismTabRepository;

    private final ZkyMechanismTabMapper zkyMechanismTabMapper;

    public ZkyMechanismTabServiceImpl(ZkyMechanismTabRepository zkyMechanismTabRepository, ZkyMechanismTabMapper zkyMechanismTabMapper) {
        this.zkyMechanismTabRepository = zkyMechanismTabRepository;
        this.zkyMechanismTabMapper = zkyMechanismTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyMechanismTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyMechanismTab> page = zkyMechanismTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyMechanismTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyMechanismTabDto> queryAll(ZkyMechanismTabQueryCriteria criteria){
        return zkyMechanismTabMapper.toDto(zkyMechanismTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyMechanismTabDto findById(Long mechanismId) {
        ZkyMechanismTab zkyMechanismTab = zkyMechanismTabRepository.findById(mechanismId).orElseGet(ZkyMechanismTab::new);
        ValidationUtil.isNull(zkyMechanismTab.getMechanismId(),"ZkyMechanismTab","mechanismId",mechanismId);
        return zkyMechanismTabMapper.toDto(zkyMechanismTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyMechanismTabDto create(ZkyMechanismTab resources) {
        return zkyMechanismTabMapper.toDto(zkyMechanismTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyMechanismTab resources) {
        ZkyMechanismTab zkyMechanismTab = zkyMechanismTabRepository.findById(resources.getMechanismId()).orElseGet(ZkyMechanismTab::new);
        ValidationUtil.isNull( zkyMechanismTab.getMechanismId(),"ZkyMechanismTab","id",resources.getMechanismId());
        zkyMechanismTab.copy(resources);
        zkyMechanismTabRepository.save(zkyMechanismTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long mechanismId : ids) {
            zkyMechanismTabRepository.deleteById(mechanismId);
        }
    }

    @Override
    public void download(List<ZkyMechanismTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyMechanismTabDto zkyMechanismTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" mechanismName",  zkyMechanismTab.getMechanismName());
            map.put("logos", zkyMechanismTab.getLogo());
            map.put(" mainPersonName",  zkyMechanismTab.getMainPersonName());
            map.put(" mobileNo",  zkyMechanismTab.getMobileNo());
            map.put("1:active 2:inactive", zkyMechanismTab.getStatus());
            map.put("delete time", zkyMechanismTab.getDeleteAt());
            map.put(" createTime",  zkyMechanismTab.getCreateTime());
            map.put(" updateTime",  zkyMechanismTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}